import React, { FunctionComponent } from 'react';
import { graphql, useFragment } from 'react-relay';
import { GenericAttack, GenericAttackCard } from '../../common/cards/GenericAttackCard';
import { MalwareCard_node$key } from './__generated__/MalwareCard_node.graphql';

export const MalwareCardFragment = graphql`
    fragment MalwareCard_node on Malware {
      id
      name
      aliases
      entity_type
      description
      created
      modified
      is_family
      createdBy {
        name
      }
      objectMarking {
        id
        definition_type
        definition
        x_opencti_order
        x_opencti_color
      }
      objectLabel {
        id
        value
        color
      }
      malware_types
      targetedCountries: stixCoreRelationships(
        relationship_type: "targets"
        toTypes: ["Country"]
        first: 10
        orderBy: created_at
        orderMode: desc
      ) {
        edges {
          node {
            to {
              ... on Country {
                name
              }
            }
          }
        }
      }
      targetedSectors: stixCoreRelationships(
        relationship_type: "targets"
        toTypes: ["Sector"]
        first: 10
        orderBy: created_at
        orderMode: desc
      ) {
        edges {
          node {
            to {
              ... on Sector {
                name
              }
            }
          }
        }
      }
      relatedIntrusionSets: stixCoreRelationships(
        relationship_type: "uses"
        fromTypes: ["Intrusion-Set"]
        first: 10
        orderBy: created_at
        orderMode: desc
      ) {
        edges {
          node {
            from {
              ... on IntrusionSet {
                name
              }
            }
          }
        }
      }
    }
`;

interface MalwareCardProps {
  node: MalwareCard_node$key;
  onLabelClick: () => void;
  bookmarksIds?: string[];
}

const MalwareCard: FunctionComponent<MalwareCardProps> = ({
  node,
  bookmarksIds,
  onLabelClick,
}) => {
  const data = useFragment(MalwareCardFragment, node);
  return (
    <GenericAttackCard
      cardData={data as GenericAttack}
      cardLink={`/dashboard/arsenal/malwares/${data.id}`}
      entityType="Malware"
      onLabelClick={onLabelClick}
      bookmarksIds={bookmarksIds}
    />
  );
};
export default MalwareCard;
